import { message, Spin } from 'antd';
import { useEffect, useRef, useState } from 'react';
// import { useModel } from 'umi';
import type { CreateLogisticsType } from '../data';
import type { ProFormInstance } from '@ant-design/pro-form';
import { ModalForm, ProFormDigit, ProFormRadio, ProFormText } from '@ant-design/pro-form';
import { logisticsConfigDetail, updateLogisticsConfig } from '../service';

interface undateParkList {
  visible: boolean;
  unVisible: () => void;
  reloadFunc: () => void;
  id: string;
}
export default (props: undateParkList) => {
  const { visible, unVisible, reloadFunc, id } = props;

  const [loading, setLoading] = useState<boolean>(true);
  const formRef = useRef<ProFormInstance>();
  const compareArray = (fW: number, wN: number[]) => {
    if (wN[0] > fW) {
      for (let i = 1; i < wN.length; i++) {
        if (wN[i - 1] > wN[i]) {
          return false;
        }
      }
      return true;
    } else {
      return false;
    }
  };
  useEffect(() => {
    logisticsConfigDetail(id).then((res) => {
      let interval = res.data.weightInterval[0];
      for (let i = 1; i < res.data.weightInterval.length; i++) {
        interval = interval + '/' + res.data.weightInterval[i];
      }
      const FieldsValue = {
        ...res.data,
        weight: interval,
      };
      formRef.current?.setFieldsValue(FieldsValue);
      setLoading(false);
    });
  }, [id]);
  return (
    <div>
      <ModalForm
        formRef={formRef}
        style={{ marginLeft: 65 }}
        modalProps={{
          destroyOnClose: true,
          onCancel: () => {
            unVisible();
          },
          centered: true,
          footer: null,
        }}
        layout="horizontal"
        onFinish={async (value: CreateLogisticsType) => {
          const { weight } = value;
          const weightString = String(weight).split('/');
          const weightNumber = [];
          for (let i = 0; i < weightString.length; i++) {
            for (let j = 0; j < weightString[i].length; j++) {
              if (!/^[0-9]$/.test(weightString[i][j])) {
                message.error(`重量区间格式不正确，请以'数字/数字/数字'的形式输入`);
                return false;
              }
            }
            weightNumber[i] = Number(weightString[i]);
          }
          const values = {
            ...value,
            _id: id,
            weightInterval: weightNumber,
          };

          if (compareArray(value.startingWeight, values.weightInterval)) {
            const hide = message.loading('正在更新');
            const res = await updateLogisticsConfig(values);
            hide();
            if (res.code === 0) {
              message.success(`更新成功`);
              reloadFunc();
              unVisible();
              return true;
            } else {
              message.error(`更新失败,${res.message}`);
              return false;
            }
          } else {
            message.error('起重重量大于区间重量或区间重量输入错误，请重新输入');
            return false;
          }
        }}
        visible={visible}
        title="添加物流配置"
        width={650}
      >
        <Spin spinning={loading}>
          <ProFormText
            required
            width={377}
            label="物流名称"
            placeholder="请输入物流名称"
            name="logisticsName"
          />
          <ProFormDigit
            fieldProps={{
              addonAfter: 'kg',
              precision: 2,
            }}
            width={377}
            required
            label="起步重量"
            placeholder="请输入起步重量"
            name="startingWeight"
          />
          <ProFormText
            required
            width={377}
            label="重量区间"
            placeholder="除首重外每个区间的末尾重量 以/间隔输入"
            name="weight"
          />
          <ProFormDigit
            fieldProps={{
              addonAfter: '折',
              precision: 1,
            }}
            width={377}
            max={10}
            min={0.1}
            initialValue={10}
            required
            label="省内打折"
            name="provinceDiscount"
          />
          <ProFormDigit
            min={0.1}
            max={10}
            fieldProps={{
              addonAfter: '折',
              precision: 1,
            }}
            width={377}
            initialValue={10}
            required
            label="省外打折"
            name="outsideDiscount"
          />
          <ProFormRadio.Group
            radioType="radio"
            width={377}
            name="chargeType"
            label="是否有最低收费"
            // fieldRef={Parktype}
            options={[
              {
                label: '没有最低收费',
                value: 1,
              },
              {
                label: '有最低收费',
                value: 2,
              },
            ]}
            rules={[{ required: true, message: '请选择收费方式' }]}
          />
          <ProFormRadio.Group
            radioType="radio"
            width={377}
            name="chargeWay"
            label="收费方式"
            // fieldRef={Parktype}
            options={[
              {
                label: '分段收费',
                value: 1,
              },
              {
                label: '对段加首重',
                value: 2,
              },
            ]}
            rules={[{ required: true, message: '请选择车场类型' }]}
          />
          <ProFormRadio.Group
            radioType="radio"
            name="weightRules"
            width={377}
            label="重量进位规则"
            // fieldRef={Parktype}
            options={[
              {
                label: '无规则',
                value: 1,
              },
              {
                label: '四舍五入取整数',
                value: 2,
              },
              {
                label: '进位至下一个0.5kg',
                value: 3,
              },
            ]}
            rules={[{ required: true, message: '请选择车场类型' }]}
          />
          <ProFormRadio.Group
            radioType="radio"
            width={377}
            name="shippingRules"
            label="运费进位规则"
            // fieldRef={Parktype}
            options={[
              {
                label: '无规则',
                value: 1,
              },
              {
                label: '四舍五入取整数',
                value: 2,
              },
            ]}
            rules={[{ required: true, message: '请选择车场类型' }]}
          />
        </Spin>
      </ModalForm>
    </div>
  );
};
